#include "../mqserver/mq_consumer.hpp"
#include "../mqcommon/mq_msg.pb.h"

#include <gtest/gtest.h>

mq::ConsumerManager::ptr cmp;
class ConsumerTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        cmp = std::make_shared<mq::ConsumerManager>();
        cmp->initQueueConsumer("queue1");
    }

    virtual void TearDown() override
    {
        
    }
};

void cb(const std::string &qname,const mq::BasicProperties *bp,const std::string &body)
{
    DLOG("%s 消费了一条数据",qname.c_str());    
}

TEST(consumer_test,insert_test)
{
    ASSERT_EQ(cmp->empty("queue1"),true);

    cmp->create("queue1","consumer1",false,cb);
    cmp->create("queue1","consumer2",false,cb);
    cmp->create("queue1","consumer3",false,cb);

    ASSERT_EQ(cmp->empty("queue1"),false);
    ASSERT_EQ(cmp->exists("consumer1","queue1"),true);
    ASSERT_EQ(cmp->exists("consumer2","queue1"),true);
    ASSERT_EQ(cmp->exists("consumer3","queue1"),true);
}

TEST(consumer_test,remove_test)
{
    cmp->remove("consumer1","queue1");
    ASSERT_EQ(cmp->empty("queue1"),false);
    ASSERT_EQ(cmp->exists("consumer1","queue1"),false);
    ASSERT_EQ(cmp->exists("consumer2","queue1"),true);
    ASSERT_EQ(cmp->exists("consumer3","queue1"),true);
}

TEST(consumer_test,choose_test)
{
    mq::Consumer::ptr csp =  cmp->choose("queue1");
    ASSERT_NE(csp.get(),nullptr);
    ASSERT_EQ(csp->_ctag,"consumer2");

    mq::Consumer::ptr csp2 =  cmp->choose("queue1");
    ASSERT_EQ(csp2->_ctag,"consumer3");

    mq::Consumer::ptr csp3 =  cmp->choose("queue1");
    ASSERT_EQ(csp3->_ctag,"consumer2");

}


int main(int argc, char *argv[])
{
    testing::AddGlobalTestEnvironment(new ConsumerTest);
    testing::InitGoogleTest(&argc, argv);
    RUN_ALL_TESTS();
    return 0;
}